program drop _all

/* THIS PROGRAM IMPLEMENTS SURVIVAL -LOGNORMAL MODEL REGRESSIONS IN TABLE 8 (cols.3-4)
   NOTE: 
   - All the source and results files are saved at single directory: C:\RESTAT\
   - This program and data were written such that all can be run IC STATA-ver. 10.1. 
*/
  

program define DUR
clear all
set more off
set memory 90m
set matsize 800
use C:\RESTAT\data1.dta, replace
log using C:\RESTAT\Tab8_survival.log, replace                    


 #delimit ; 

  
                       tsset firm year ;
                       sort primaryu ;
                       merge primaryu using C:\RESTAT\export_mysample.dta  ;  
                                                 /* contains indicators which industries belong to those 2 SKP=CPA
                                                    (Czech Production Activity) classification industry groups with 
                                                    at least 1% (exp1=1)--> 72 industries
                                                 */

                       drop _merge ;

                      display" In case of 2 observations  missing, because no data there on sales in year 1994";
                      display"Replace these by IndG==0";
                      replace IndG=0 if primaryu==175 & year==1994;   

/* KEEP SAMPLE WITH NON MISSING OBS. FOR EXIT EVEN THOSE WHICH MISSING FOR GROWTH */                       
                       keep if EI~=. & FG~=. & IndG~=. & ageT~=. & sales~=. & AS~=.& ES~=.& FORdirect~=. &
                          intang~=. & ESgap~=. & gap~=. & solvency~=. ; 


/* SET DATA FOR DURATION ANALYSIS */

                    sort firm;
                    by firm: egen MIN_age=min(age);
                    by firm: egen MIN_ageT=min(ageT);
   
                    tsset firm year ;
                   

                     /* USING AGE_T */
                    gen MAT=MIN_ageT-1 ;
                    stset ageT, id(firm) failure(EI==1) entry(MAT) ;                 

 /* DROP YEAR 2000 - THE CZECH EXPORT INFO PROVIDED ONLY UNTIL YR 2000 */
                  
                     drop if year==2000; 

/* RESTRICT SAMPLE TO GIVEN SUB-SAMPLE BEFORE CALCULATING FIRM LEVEL MEANS */
                    display" HIGH EXPORT INDUSTRIES: EXP>=1%";
                    keep if exp1==1;
  
                     
/* TO AVOID PERFECT FAILURE/SUCCESS DETERMINATION PROBLEM RE-GROUP DUMMIES - APPLY RE-GROUPINGS FROM TAB4 FIRST */            

                   replace primaryu=16 if US2==16;
                   replace primaryu=17 if US2==17;
                   replace primaryu=203 if (primaryu==204|primaryu==205);
                   replace primaryu=208 if primaryu==209;
                   replace primaryu=22 if (US2==22|US2==23);
                   replace primaryu=24 if  US2==24;
                   replace primaryu=25 if  US2==25;
                   replace primaryu=26 if (US2==26|US2==27|US2==28);
                   replace primaryu=317 if (primaryu==322|primaryu==325|primaryu==326|
                                           primaryu==327|primaryu==328);
                   replace primaryu=332 if primaryu==333;
                   replace primaryu=343 if primaryu==344;
                   replace primaryu=345 if primaryu==346;
                   replace primaryu=351 if primaryu==352;
                   replace primaryu=361 if primaryu==362;
                   replace primaryu=363 if primaryu==364;
                   replace primaryu=374 if primaryu==375;
                   replace primaryu=38 if (US2==38|US2==39);
                   replace primaryu=47 if (primaryu==472|primaryu==481|primaryu==491|primaryu==493);
                   replace primaryu=505 if (primaryu==506|primaryu==507|primaryu==508);
                   replace primaryu=515 if primaryu==516;
                   replace primaryu=519 if primaryu==523;
                   replace primaryu=554 if (primaryu==571|primaryu==581);
                   replace primaryu=591 if (primaryu==593|primaryu==594);
                   replace primaryu=621 if primaryu==628;
                   replace primaryu=721 if primaryu==729;
                   replace primaryu=735 if primaryu==737;
                   replace primaryu=792 if (primaryu==794|primaryu==799);                      
                   
                
                    display" GROUP YEAR DUMMIES INTO 2 YEARS (94+95), (96+97), WHERE PERFECT FAIULRE DETERM. PROBLEM";
                    replace year94=1 if (year==1994|year==1995);
                    replace year97=1 if (year==1996|year==1997);
                    replace year00=1 if (year==2000|year==2001);
                    drop year95 year96 ; /* 2 years when perfect failure determination  */
                    /* so now yearly dummies are: 94,97, 98, 99,00 and keep year94 as a base group, so year97-00 */


                    display" FOR THE SIMILAR REASONS REPLACE ageT WITH 12 IF ageT==13 ";
                    replace ageT=12 if ageT==13;  
  
  
         
 /* ADDITIONAL REGROUPINGS IF EXPORT>1% FOR REGION AND YEAR */
                    
                    /* REGION REGROUPINGS EXP >=1% */
                    display" FOR THE SAME REASON MERGE REGION=NA WITH PRAGUE ";  
                    replace reg5=1 if reg8==1;
                    drop reg8;  /* region dummies are: reg1-reg7 and 1 group will be omitted*/
                   
                    /* YEAR REGROUPINGS */
                    replace year97=1 if (year==1996|year==1997|year==1998);
                    drop year98;
                                   /* so now yearly dummies are: 94,97, 99,00 */

                    replace primaryu=347 if primaryu==345;
                    replace primaryu=371 if primaryu==372;
                                        


 /*  CALCULATE THE FIRM LEVEL MEANS ONLY FOR A RELEVANT TIME PERIOD */ 
                       
                    sort firm ;
                    by firm: egen avsales=mean(sales);
                    by firm: egen avsales2=mean(sales2);
                    by firm: egen avESgap=mean(ESgap);
                    by firm: egen avFG=mean(FG);
                    by firm: egen avageT=mean(ageT);
                    by firm: egen avageT2=mean(ageT2);
                    by firm: egen avAS=mean(AS);
                    by firm: egen avES=mean(ES);
                    by firm: egen avFORdirect=mean(FORdirect);
                    by firm: egen avintang=mean(intang);
                    by firm: egen avgap=mean(gap);
                    by firm: egen avsolvency=mean(solvency);                    
                       
                    tsset firm year ;                 
                                    
                
    
 

         
 /* **************************** LOGNORMAL: Tab 8 (col.3) HIGH EXPORT INDUSTRIES (EXPORT>=1%) ************ */                       
 
                xi:streg FG STATIC sales sales2 ES FORdirect intang gap ESgap D89 
                        solvency NO_FOR year94-year00 i.primaryu reg1-reg7 IndG 
                        avFG avsales avsales2 avES avFORdirect avintang 
                        avgap avESgap avsolvency, dist(lnormal) cluster(firm) tr;
                                                    
  


/* *********************** REPEAT THE ANALYSIS NOW FOR THE LOW EXPORT INDUSTRIES (EXPORT<1%) ******************* */

clear;        
use C:\RESTAT\data1.dta, replace;

                   
                       tsset firm year ;
                       sort primaryu ;
                       merge primaryu using C:\RESTAT\export_mysample.dta  ;  
                                                 /* contains indicators which industries belong to those 2 SKP=CPA
                                                    (Czech Production Activity) classification industry groups with 
                                                    at least 1% (exp1=1)--> 72 industries
                                                   */

                       drop _merge ;

                      display" In case of 2 observations  missing, because no data there on sales in year 1994";
                      display"Replace these by IndG==0";
                      replace IndG=0 if primaryu==175 & year==1994;   

/* KEEP SAMPLE WITH NON MISSING OBS. FOR EXIT EVEN THOSE WHICH MISSING FOR GROWTH */                       
                       keep if EI~=. & FG~=. & IndG~=. & ageT~=. & sales~=. & AS~=.& ES~=.& FORdirect~=. &
                          intang~=. & ESgap~=. & gap~=. & solvency~=. ; 


/* SET DATA FOR DURATION ANALYSIS */

                    sort firm;
                    by firm: egen MIN_age=min(age);
                    by firm: egen MIN_ageT=min(ageT);
   
                    tsset firm year ;
                   

                     /* USING AGE_T */
                    gen MAT=MIN_ageT-1 ;
                    stset ageT, id(firm) failure(EI==1) entry(MAT) ;                 

 /* DROP YEAR 2000 - THE CZECH EXPORT INFO PROVIDED ONLY UNTIL YR 2000 */
                  
                     drop if year==2000; 

/* RESTRICT SAMPLE TO GIVEN SUB-SAMPLE BEFORE CALCULATING FIRM LEVEL MEANS */
                    display" LOW EXPORT INDUSTRIES: EXP<1%";
                    keep if exp1==.;
  
                     
/* TO AVOID PERFECT FAILURE/SUCCESS DETERMINATION PROBLEM RE-GROUP DUMMIES - APPLY RE-GROUPINGS FROM TAB4 FIRST */            

                   replace primaryu=16 if US2==16;
                   replace primaryu=17 if US2==17;
                   replace primaryu=203 if (primaryu==204|primaryu==205);
                   replace primaryu=208 if primaryu==209;
                   replace primaryu=22 if (US2==22|US2==23);
                   replace primaryu=24 if  US2==24;
                   replace primaryu=25 if  US2==25;
                   replace primaryu=26 if (US2==26|US2==27|US2==28);
                   replace primaryu=317 if (primaryu==322|primaryu==325|primaryu==326|
                                           primaryu==327|primaryu==328);
                   replace primaryu=332 if primaryu==333;
                   replace primaryu=343 if primaryu==344;
                   replace primaryu=345 if primaryu==346;
                   replace primaryu=351 if primaryu==352;
                   replace primaryu=361 if primaryu==362;
                   replace primaryu=363 if primaryu==364;
                   replace primaryu=374 if primaryu==375;
                   replace primaryu=38 if (US2==38|US2==39);
                   replace primaryu=47 if (primaryu==472|primaryu==481|primaryu==491|primaryu==493);
                   replace primaryu=505 if (primaryu==506|primaryu==507|primaryu==508);
                   replace primaryu=515 if primaryu==516;
                   replace primaryu=519 if primaryu==523;
                   replace primaryu=554 if (primaryu==571|primaryu==581);
                   replace primaryu=591 if (primaryu==593|primaryu==594);
                   replace primaryu=621 if primaryu==628;
                   replace primaryu=721 if primaryu==729;
                   replace primaryu=735 if primaryu==737;
                   replace primaryu=792 if (primaryu==794|primaryu==799);                      
                   
                
                    display" GROUP YEAR DUMMIES INTO 2 YEARS (94+95), (96+97), WHERE PERFECT FAIULRE DETERM. PROBLEM";
                    replace year94=1 if (year==1994|year==1995);
                    replace year97=1 if (year==1996|year==1997);
                    replace year00=1 if (year==2000|year==2001);
                    drop year95 year96 ; /* 2 years when perfect failure determination  */
                    /* so now yearly dummies are: 94,97, 98, 99,00 and keep year94 as a base group, so year97-00 */


                    display" FOR THE SIMILAR REASONS REPLACE ageT WITH 12 IF ageT==13 ";
                    replace ageT=12 if ageT==13;  /* 0 changes */
  

 /*  ADDITIONAL ADJUSTMENTS IF EXPORT<1% */
                        /* problem 2 groups: 38 and 361 only groups starting with 3, how to merge? Better to drop... */
                          drop if (primaryu==38|primaryu==361); 


 /*  CALCULATE THE FIRM LEVEL MEANS ONLY FOR A RELEVANT TIME PERIOD */ 
                       
                    sort firm ;
                    by firm: egen avsales=mean(sales);
                    by firm: egen avsales2=mean(sales2);
                    by firm: egen avESgap=mean(ESgap);
                    by firm: egen avFG=mean(FG);
                    by firm: egen avageT=mean(ageT);
                    by firm: egen avageT2=mean(ageT2);
                    by firm: egen avAS=mean(AS);
                    by firm: egen avES=mean(ES);
                    by firm: egen avFORdirect=mean(FORdirect);
                    by firm: egen avintang=mean(intang);
                    by firm: egen avgap=mean(gap);
                    by firm: egen avsolvency=mean(solvency);                    
                       
                    tsset firm year ;                 
   

    
                                 
                
/* **************************** LOGNORMAL: Tab 8 (col.4) LOW EXPORT INDUSTRIES (EXPORT<1%) ************ */                       
 
/* Note: similarly as in tobit NO_FOR dummy must be dropped in this subset, due to perfect multicollinearity */

                xi:streg FG STATIC sales sales2 ES FORdirect intang gap ESgap D89 
                        solvency year94-year00 i.primaryu reg1-reg7 IndG 
                        avFG avsales avsales2 avES avFORdirect avintang 
                        avgap avESgap avsolvency, dist(lnormal) cluster(firm) tr;
   
                

#delimit cr
log close 
end
